<?php
namespace app\api\controller;
header("Content-Type: text/html;charset=utf-8");
header('Access-Control-Allow-Origin:*');
use think\Controller;
use think\Db;
class Update extends Controller
{
    public function file(){
    $uid=input('uid');
    $fid=input('fid');
    $fname=input('fname');
    $file=request()->file('fpic');
    $pic_before=Db::name("file_ck")->where("fid='$fid'")->value('fpic');
    $delpic=unlink(ROOT_PATH.'public'.DS.'static'.DS.'Fileimages'.DS.$pic_before);

    if(!$delpic){
      return json([
                    'error'=>'0',
                    'msg'=>'删除图片失败']);
    }else{
      if ($file){
        $info=$file->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])
        ->rule('uniqid')->move(ROOT_PATH.'public'.DS.'static'.DS.'Fileimages');
        if($info){
          $date=[
                  'uid'=>$uid,
                  'fname'=>$fname,
                  'fupdatetime'=>date('Y-m-d H:i:s'),
                  'fpic'=>$info->getFilename()];
          $addpic=Db::name('file_ck')->where('fid',$fid)->update($date);
          if($addpic)
            return json([
                          'status'=>'1',
                          'msg'=>'保存成功']);
          else
            return json([
                          'error'=>'1',
                          'msg'=>'保存失败']);
        }else
          return json([
                        'status'=>'0',
                        'msg'=>$file->getError()]);
      }else
        return json([
                      'status'=>'2',
                      'msg'=>$file->getError()]);               
     }

    }

    public function user(){
      $uid=input('post.uid');
      $uname=input('post.uname');
      $uphone=input('post.uphone');
      $uemail=input('post.uemail');
      $target=Db::name('user_ck')->where('uid',$uid)->find();
      if($target){
        $data=[
                "uname"=>$uname,
                "uphone"=>$uphone,
                "uemail"=>$uemail];
        $request=Db::name("user_ck")->where("uid='$uid'")->update($data);
        if($request){
          return json([
                        "status"=>"1",
                        "msg"=>"信息修改成功"]);
        }else{
          return json([
                        "status"=>"0",
                        "msg"=>"信息修改失败"]);
        }     
      }else{
        return json([
                      "error"=>"0",
                      "msg"=>"用户ID不存在"]);
      }
  }
  public function userimage(){
    $uid=input('post.uid');
    $file=request()->file('upic');
    if (request()->isPost()) {
            if ($file) {
              $pic_before=Db::name("user_ck")->where("uid='$uid'")->value('upic');
              if (!is_null($pic_before)) {
              unlink(ROOT_PATH.'public'.DS. 'static'.DS.'Userimages'.DS.$pic_before);
              }

              $info = $file->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->
              rule('uniqid')->move(ROOT_PATH.'public'.DS. 'static'.DS.'Userimages');
                  if ($info) {
                              $data=[
                                "upic"=>$info->getFilename()
                              ];
                        $request=Db::name("user_ck")->where("uid='$uid'")->update($data);
                        if ($request) {
                          return json([
                                  "status"=>"1",
                                  "msg"=>"修改成功"
                          ]);
                        } else {
                          return json([
                                 "status"=>"0",
                                 "msg"=>"修改失败"
                          ]);
                        }
                             
                          } else {
                            echo $file->getError();
                          }
              
            } else {
              return json([
                  "error"=>"0",
                  "msg"=>"文件上传失败"
              ]);
            }
    
    }else{
      return json([
                  "error"=>"1",
                  "msg"=>"未使用Post方法"
              ]);
    }
}


}